//管理用户数据相关
import {defineStore} from "pinia";
import {ref} from "vue";
import {addItemToCartAPI, deleteCartItemAPI,queryMyCartsAPI,updateCartAPI} from "@/api/cart";

export const useCartStore=defineStore('cart',()=>{
    const cartList=ref({})

    const getCartList = async (businessId) => {
        const res=await queryMyCartsAPI(businessId)
        cartList.value=res
        return res;
    }

    const saveCart = async (cartFormDTO) => {
        await addItemToCartAPI(cartFormDTO)
    }

    const updateCart=async (cart)=>{
        const res=await updateCartAPI(cart)
        return res
    }

    const removeCart =async(businessId,itemId) => {
      await deleteCartItemAPI({businessId,itemId})
    }


    //3. 以对象的格式把state和action return
    return{
        cartList,
        saveCart,
        updateCart,
        removeCart,
        getCartList,
    }
},{
    persist:true
})
